﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using Newtonsoft.Json;
using System.Configuration;
using System.Data;

namespace PromotionEngine
{
    public partial class PromotionDetail : System.Web.UI.Page
    {
        public class Response
        {
            public Model.PromotionHeader Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(Model.PromotionHeader data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        [WebMethod]
        public static string GetPromotionHeaderDetail(string promotionID)
        {
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetPromotionHeaderByID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), promotionID);
            if (dsPromotion != null)
            {
                Model.PromotionHeader promotionheader = new Model.PromotionHeader();
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    promotionheader.PromotionID = r["PromotionID"].ToString();
                    promotionheader.CompanyCode = r["CompanyCode"].ToString();
                    promotionheader.PromotionCode = r["PromotionCode"].ToString();
                    promotionheader.PromotionName = r["PromotionName"].ToString();
                    promotionheader.FatherPromotionCode = r["FatherPromotionCode"].ToString();
                    promotionheader.PromotionType = r["PromotionType"].ToString();
                    if (r["FromDate"].ToString().Length > 0)
                    {
                        promotionheader.FromDate = DateTime.Parse(r["FromDate"].ToString());
                    }
                    if (r["ToDate"].ToString().Length > 0)
                    {
                        promotionheader.ToDate = DateTime.Parse(r["ToDate"].ToString());
                    }
                    if (r["FromTime"].ToString().Length > 0)
                    {
                        promotionheader.FromTime = DateTime.Parse(r["FromTime"].ToString());
                    }
                    if (r["ToTime"].ToString().Length > 0)
                    {
                        promotionheader.ToTime = DateTime.Parse(r["ToTime"].ToString());
                    }
                    if (r["IsActive"].ToString().Length > 0)
                    {
                        promotionheader.IsActive = bool.Parse(r["IsActive"].ToString());
                    }
                    else
                    {
                        promotionheader.IsActive = true;
                    }
                    if (r["IsOpional"].ToString().Length > 0)
                    {
                        promotionheader.IsOpional = bool.Parse(r["IsOpional"].ToString());
                    }
                    if (r["FromAmount"].ToString().Length > 0)
                    {
                        promotionheader.FromAmount = double.Parse(r["FromAmount"].ToString());
                    }
                    if (r["ToAmount"].ToString().Length > 0)
                    {
                        promotionheader.ToAmount = double.Parse(r["ToAmount"].ToString());
                    }
                    if (r["FromQuantity"].ToString().Length > 0)
                    {
                        promotionheader.FromQuantity = double.Parse(r["FromQuantity"].ToString());
                    }
                    if (r["ToQuantity"].ToString().Length > 0)
                    {
                        promotionheader.ToQuantity = double.Parse(r["ToQuantity"].ToString());
                    }
                    if (r["DiscountType"].ToString().Length > 0)
                    {
                        promotionheader.DiscountType = r["DiscountType"].ToString();
                    }
                    if (r["DiscountValue"].ToString().Length > 0)
                    {
                        promotionheader.DiscountValue = double.Parse(r["DiscountValue"].ToString());
                    }
                    if (r["MaxCountApply"].ToString().Length > 0)
                    {
                        promotionheader.MaxCountApply = int.Parse(r["MaxCountApply"].ToString());
                    }
                    if (r["Monday"].ToString().Length > 0)
                    {
                        promotionheader.Monday = bool.Parse(r["Monday"].ToString());
                    }
                    else
                    {
                        promotionheader.Monday = true;
                    }
                    if (r["MondayStart"].ToString().Length > 0)
                    {
                        promotionheader.MondayStart = DateTime.Parse(r["MondayStart"].ToString());
                    }
                    if (r["MondayEnd"].ToString().Length > 0)
                    {
                        promotionheader.MondayEnd = DateTime.Parse(r["MondayEnd"].ToString());
                    }
                    if (r["Tuesday"].ToString().Length > 0)
                    {
                        promotionheader.Tuesday = bool.Parse(r["Tuesday"].ToString());
                    }
                    else
                    {
                        promotionheader.Tuesday = true;
                    }
                    if (r["TuesdayStart"].ToString().Length > 0)
                    {
                        promotionheader.TuesdayStart = DateTime.Parse(r["TuesdayStart"].ToString());
                    }
                    if (r["TuesdayEnd"].ToString().Length > 0)
                    {
                        promotionheader.TuesdayEnd = DateTime.Parse(r["TuesdayEnd"].ToString());
                    }
                    if (r["Wednesday"].ToString().Length > 0)
                    {
                        promotionheader.Wednesday = bool.Parse(r["Wednesday"].ToString());
                    }
                    else
                    {
                        promotionheader.Wednesday = true;
                    }
                    if (r["WednesdayStart"].ToString().Length > 0)
                    {
                        promotionheader.WednesdayStart = DateTime.Parse(r["WednesdayStart"].ToString());
                    }
                    if (r["WednesdayEnd"].ToString().Length > 0)
                    {
                        promotionheader.WednesdayEnd = DateTime.Parse(r["WednesdayEnd"].ToString());
                    }
                    if (r["Thursday"].ToString().Length > 0)
                    {
                        promotionheader.Thursday = bool.Parse(r["Thursday"].ToString());
                    }
                    else
                    {
                        promotionheader.Thursday = true;
                    }
                    if (r["ThursdayStart"].ToString().Length > 0)
                    {
                        promotionheader.ThursdayStart = DateTime.Parse(r["ThursdayStart"].ToString());
                    }
                    if (r["ThursdayEnd"].ToString().Length > 0)
                    {
                        promotionheader.ThursdayEnd = DateTime.Parse(r["ThursdayEnd"].ToString());
                    }
                    if (r["Friday"].ToString().Length > 0)
                    {
                        promotionheader.Friday = bool.Parse(r["Friday"].ToString());
                    }
                    else
                    {
                        promotionheader.Friday = true;
                    }
                    if (r["FridayStart"].ToString().Length > 0)
                    {
                        promotionheader.FridayStart = DateTime.Parse(r["FridayStart"].ToString());
                    }
                    if (r["FridayEnd"].ToString().Length > 0)
                    {
                        promotionheader.FridayEnd = DateTime.Parse(r["FridayEnd"].ToString());
                    }
                    if (r["Saturday"].ToString().Length > 0)
                    {
                        promotionheader.Saturday = bool.Parse(r["Saturday"].ToString());
                    }
                    else
                    {
                        promotionheader.Saturday = true;
                    }
                    if (r["SaturdayStart"].ToString().Length > 0)
                    {
                        promotionheader.SaturdayStart = DateTime.Parse(r["SaturdayStart"].ToString());
                    }
                    if (r["SaturdayEnd"].ToString().Length > 0)
                    {
                        promotionheader.SaturdayEnd = DateTime.Parse(r["SaturdayEnd"].ToString());
                    }
                    if (r["Sunday"].ToString().Length > 0)
                    {
                        promotionheader.Sunday = bool.Parse(r["Sunday"].ToString());
                    }
                    else
                    {
                        promotionheader.Sunday = true;
                    }
                    if (r["SundayStart"].ToString().Length > 0)
                    {
                        promotionheader.SundayStart = DateTime.Parse(r["SundayStart"].ToString());
                    }
                    if (r["SundayEnd"].ToString().Length > 0)
                    {
                        promotionheader.SundayEnd = DateTime.Parse(r["SundayEnd"].ToString());
                    }
                    promotionheader.CreatedUserID = r["CreatedUserID"].ToString();
                    if (r["CreatedDate"].ToString().Length > 0)
                    {
                        promotionheader.CreatedDate = DateTime.Parse(r["CreatedDate"].ToString());
                    }
                    if (r["BudgetAmount"].ToString().Length > 0)
                    {
                        promotionheader.BudgetAmount = double.Parse(r["BudgetAmount"].ToString());
                    }
                    if (r["TargetRevenue"].ToString().Length > 0)
                    {
                        promotionheader.TargetRevenue = double.Parse(r["TargetRevenue"].ToString());
                    }
                    if (r["EveryAt"].ToString().Length > 0)
                    {
                        promotionheader.EveryAt = int.Parse(r["EveryAt"].ToString());
                    }
                }
                Response result = new Response(promotionheader, dsPromotion.Tables[0].Rows.Count);
                return JsonConvert.SerializeObject(result);
            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetAllReferenceValue()
        {
            List<Model.ReferenceTypeValue> lstReferenceValue = new List<Model.ReferenceTypeValue>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetReferenceValue(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), "DiscountType");
            if (dsPromotion != null)
            {
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);
            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static List<Model.ReferenceTypeValue> GetAllReferenceValueDiscount()
        {
            List<Model.ReferenceTypeValue> lstReferenceValue = new List<Model.ReferenceTypeValue>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetReferenceValue(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), "DiscountType");
            if (dsPromotion != null && dsPromotion.Tables.Count > 0)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.ReferenceTypeValue referenceValue = new Model.ReferenceTypeValue();
                    referenceValue.ID = r["ID"].ToString();
                    referenceValue.ReferenceCode = r["ReferenceCode"].ToString();
                    referenceValue.ReferenceName = r["ReferenceName"].ToString();
                    referenceValue.ReferenceValue = r["ReferenceValue"].ToString();
                    lstReferenceValue.Add(referenceValue);
                }

            }
            return lstReferenceValue;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static List<Model.ReferenceTypeValue> GetAllReferenceValuePromotion()
        {
            List<Model.ReferenceTypeValue> lstReferenceValue = new List<Model.ReferenceTypeValue>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetReferenceValue(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), "PromotionType");
            if (dsPromotion != null && dsPromotion.Tables.Count > 0)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.ReferenceTypeValue referenceValue = new Model.ReferenceTypeValue();
                    referenceValue.ID = r["ID"].ToString();
                    referenceValue.ReferenceCode = r["ReferenceCode"].ToString();
                    referenceValue.ReferenceName = r["ReferenceName"].ToString();
                    referenceValue.ReferenceValue = r["ReferenceValue"].ToString();
                    lstReferenceValue.Add(referenceValue);
                }

            }
            return lstReferenceValue;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static string UpdatePromotionToDB(string promotionDetail, bool isInsert)
        {
            Model.PromotionHeader header = JsonConvert.DeserializeObject<Model.PromotionHeader>(promotionDetail);
            if (header != null)
            {
                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        header.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                        header.CreatedDate = DateTime.Now;
                        header.CompanyCode = tbUser.Rows[0]["CompanyCode"].ToString();
                    }
                }
                Promotion pmt = new Promotion();
                string errMsg = pmt.UpdateJsonPromotionHeader(header.PromotionID, header.CompanyCode, header.PromotionCode, header.PromotionName, header.FatherPromotionCode,
                    header.PromotionType, header.FromDate.Value, header.ToDate.Value, header.FromTime.Value, header.ToTime.Value, header.IsActive.Value,
                    header.IsOpional.Value, header.FromAmount.Value, header.ToAmount.Value,
                    header.FromQuantity.Value, header.ToQuantity.Value, header.DiscountType, header.DiscountValue.Value, header.MaxCountApply.Value,
                    header.Monday.Value, header.MondayStart.Value, header.MondayEnd.Value, header.Tuesday.Value, header.TuesdayStart.Value, header.TuesdayEnd.Value,
                    header.Wednesday.Value, header.WednesdayStart.Value, header.WednesdayEnd.Value,
                    header.Thursday.Value, header.ThursdayStart.Value, header.ThursdayEnd.Value, header.Friday.Value, header.FridayStart.Value, header.FridayEnd.Value,
                    header.Saturday.Value, header.SaturdayStart.Value, header.SaturdayEnd.Value, header.Sunday.Value, header.SundayStart.Value, header.SundayEnd.Value,
                    header.CreatedUserID, header.CreatedDate.Value, header.BudgetAmount.Value, header.TargetRevenue.Value, header.EveryAt.Value,
                    ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
                return errMsg;
            }
            return "0";
        }
        [WebMethod]
        public static string GetLastRecord()
        {
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetLastRecordPromotionHeader(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
            if (dsPromotion != null)
            {
                return JsonConvert.SerializeObject(dsPromotion);
            }
            return string.Empty;
        }
    }
}
